Skip to content

Comments

add default content type for empty requests #275

Open
margaretjgu wants to merge 1 commit intomainfrom
content-type
Open

add default content type for empty requests #275
margaretjgu wants to merge 1 commit intomainfrom
content-type

Conversation

@margaretjgu
Copy link
Member

Closes elastic/elasticsearch-py#3305
Elasticsearch 9.1+ will request requests with no content-types.
Python transport layer only includes content-type when a request body is present but bodyless requests will have no content-type.

Bodyless requests will now get application/json as default

+ resolve_default(client_meta, ())
)

request_headers.setdefault("content-type", "application/json")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

generated apis already set content-type: application/json or x-ndjson for e.g. bulk requests before calling transport.

so setdefault is a no-op in this case, so the predefined content-type is preserved.

eg. bulk request in client where content-type is always set
https://github.com/elastic/elasticsearch-py/blob/c485ee48177892efd8083553ebf14bfaa804e59b/elasticsearch/_sync/client/__init__.py#L749-L752

e.g. where content-type is optionally set if body is present like search
https://github.com/elastic/elasticsearch-py/blob/f59037ee0e9de82719dce6ba8b56b9022d60ee0c/elasticsearch/_sync/client/watcher.py#L337-L341

@miguelgrinberg
Copy link
Contributor

Mostly nitpicking, but this introduces a small behavior change. Forgetting to set the content type for a request with a body would trigger an error, which is, I guess, a good thing. With this change this would not happen anymore, and the request would silently get a JSON content type.

Another way to handle this without changing the original behavior would be to explicitly set the content type in the few endpoints that support not having a body. This could be done in the code generator.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make sure Content-Type is set with empty body requests

2 participants